package com.zds.support.widget

import android.content.Context
import android.util.AttributeSet
import android.webkit.WebView
import java.util.HashMap

/**
 *webview
 *@Description
 **/
class EasyWebView @JvmOverloads constructor(context: Context,
                                            attrs: AttributeSet? = null,
                                            defStyleAttr: Int = 0)
    : WebView(context, attrs, defStyleAttr) {

    private var onScrollChangeListener: OnScrollChangeListener? = null
    private var defaultHandler: BridgeHandler = DefaultHandler()
    private var messageHandlers: MutableMap<String, BridgeHandler> = HashMap()

    fun setOnScrollChangeListener(listener: OnScrollChangeListener) {
        this.onScrollChangeListener = listener
    }

    fun setDefaultHandler(handler: BridgeHandler) {
        this.defaultHandler = handler
    }

    /**
     * register handler,so that javascript can call it
     *
     * @param handlerName
     * @param handler
     */
    fun registerHandler(handlerName: String, handler: BridgeHandler?) {
        if (handler != null) {
            messageHandlers.put(handlerName, handler)
        }
    }

    interface OnScrollChangeListener {
        public fun onPageEnd(l: Int, t: Int, oldl: Int, oldt: Int);

        public fun onPageTop(l: Int, t: Int, oldl: Int, oldt: Int);

        public fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int);

    }

    interface BridgeHandler {
        fun handler(data: String, function: CallBackFunction)
    }

    interface CallBackFunction {
        fun onCallBack(data: String)
    }

    class DefaultHandler : BridgeHandler {
        override fun handler(data: String, function: CallBackFunction) {
            function.onCallBack("DefaultHandler response data")
        }

    }

    override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
        super.onScrollChanged(l, t, oldl, oldt)
        onScrollChangeListener?.onScrollChanged(l, t, oldl, oldt)


    }
}